package com.itheima.health.jobs;

import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.DateUtils;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
////3、定时器已经基本满足了基本业务的开发，
// 但是，由于定时器的执行优先于注入，因此我们不能通过@Resource注入service。

public class ClearOrderSetting  {
    @Reference
    private OrderSettingService orderSettingService;

public  void  clearOrderSettingBefer(){

    Calendar cal = Calendar.getInstance();

    cal.add(cal.MONTH,-1);
    Date time = cal.getTime();

    List<OrderSetting> orderSettings = orderSettingService.findListByDate(time);
   // System.out.println(orderSettings);
    if (orderSettings!=null&&orderSettings.size()>0){
        stuList2Excel(orderSettings);
        System.out.println("执行清除预约设置的操作------------------------");
        orderSettingService.delectOrdersettingByDate(time);
    }

}

    /**
     * function:导出一个月之前的预约设置到外部excel表格
     * @param orderSettings
     */
    public  void stuList2Excel(List<OrderSetting> orderSettings){
        XSSFWorkbook workbook = new XSSFWorkbook();
        //标题行抽出字段
        String[] title = {"标号","订单时间", "可预约数量", "已预约数量"};
        //设置sheet名称，并创建新的sheet对象
        String sheetName = "被删除的订单设置表";
        XSSFSheet sheet = workbook.createSheet(sheetName);
        //获取表头行
        XSSFRow rowhead = sheet.createRow(0);
        XSSFCell cell = null;
//        将标题写入表头
        for (int i = 0; i < title.length; i++) {
            cell = rowhead.createCell(i);
            cell.setCellValue(title[i]);
        }
       XSSFRow row = null;
        for (int i = 0; i < orderSettings.size(); i++) {
           row = sheet.createRow(i + 1);
            //把值一一写进单元格里
            //设置第一列为自动递增的序号
            System.out.println(orderSettings);
            try{
                Date orderDate = orderSettings.get(i).getOrderDate();
                row.createCell(0).setCellValue(i+1);
                row.createCell(1).setCellValue(DateUtils.parseDate2String(orderDate));
                row.createCell(2).setCellValue(orderSettings.get(i).getNumber());
                row.createCell(3).setCellValue(orderSettings.get(i).getReservations());
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        //获取配置文件中保存对应excel文件的路径，本地也可以直接写成F：excel/stuInfoExcel路径
        String folderPath ="D:\\预约设置表格" + File.separator + "ClearOrderSettingInfo";
        //创建上传文件目录
        File folder = new File(folderPath);
        //如果文件夹不存在创建对应的文件夹
        if (!folder.exists()) {
            folder.mkdirs();
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = format.format(new Date());
        //设置文件名
        String fileName =format1 +sheetName+ ".xlsx";
        String savePath = folderPath + File.separator + fileName;
        OutputStream fileOut = null;
        try {
            fileOut = new FileOutputStream(savePath);
            workbook.write(fileOut);
            fileOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //返回文件保存全路径

    }

}
